﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Xml.Linq;
using YArchitech.LIB;

namespace YArchitech.Temp
{
	internal class HYDuctSystemDocument
	{
		public HYDuctSystem DuctSystem { get; set; }

		public HYDuctSystemOption Option { get; set; }

		public string FileName { get; set; }

		public bool Modified { get; set; }

		public string Path { get; set; }

		public double Version
		{
			get
			{
				return (double)this.m_Version;
			}
		}

		public string ErrorString { get; set; }

		public HYDuctSpecifications Rounds
		{
			get
			{
				if (this.m_Rounds == null)
				{
					HYDataAccess.GetDuctSpecifications(out this.m_Rounds, HYShapeProfile.Round);
				}
				return this.m_Rounds;
			}
		}

		public HYDuctSpecifications Rects
		{
			get
			{
				if (this.m_Rects == null)
				{
					HYDataAccess.GetDuctSpecifications(out this.m_Rects, HYShapeProfile.Rectangular);
				}
				return this.m_Rects;
			}
		}

		public HYDuctSpecifications AvailableMainRects
		{
			get
			{
				return this.m_AvailableMainRects;
			}
		}

		public HYDuctSpecifications AvailableSideRects
		{
			get
			{
				return this.m_AvailableSideRects;
			}
		}

		public HYDuctSystemDocument()
		{
			this.Option = new HYDuctSystemOption();
			this.Option.Load(Product.UserConfigFile);
			this.DuctSystem = new HYDuctSystem();
		}

		public bool Save()
		{
			if (string.IsNullOrEmpty(this.FileName))
			{
				this.ErrorString = "文件名为空！";
				return false;
			}
			bool result;
			try
			{
				XElement xelement = new XElement("HYDuctSystemDocument", new XAttribute("Version", this.m_Version));
				XElement xelement2 = new XElement("HYDuctSystemOption");
				this.Option.WriteXml(xelement2);
				xelement.Add(xelement2);
				XElement xelement3 = new XElement("HYDuctSystem");
				xelement.Add(xelement3);
				this.DuctSystem.WriteXml(xelement3);
				xelement.Save(this.FileName);
				this.ErrorString = "文件保存成功！";
				this.Modified = false;
				result = true;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				this.ErrorString = "保存数据失败，请检查文件位置";
				result = false;
			}
			return result;
		}

		public bool Open()
		{
			if (string.IsNullOrEmpty(this.FileName) || File.Exists(this.FileName))
			{
				this.ErrorString = "文件不存在";
				return false;
			}
			bool result;
			try
			{
				XElement xelement = XElement.Load(this.FileName);
				XElement xe = xelement.Element("HYDuctSystemOption");
				this.Option.ReadXml(xe);
				XElement xe2 = xelement.Element("HYDuctSystem");
				this.DuctSystem.ReadXml(xe2);
				this.ErrorString = "打开文件成功！";
				result = true;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				this.ErrorString = "读取文件错误，请确认数据文件是否完整！";
				result = false;
			}
			return result;
		}

		public bool FilterSpecifications(out HYDuctSpecifications mainSpecs, out HYDuctSpecifications sideSpecs)
		{
			mainSpecs = null;
			sideSpecs = null;
			if (this.Rects == null || this.Rects.Count == 0)
			{
				this.ErrorString = "没有矩形风管数据，请首先设置风管管径！";
				return false;
			}
			if (this.Rounds == null || this.Rounds.Count == 0)
			{
				this.ErrorString = "没有圆形风管数据，请首先设置风管管径！";
				return false;
			}
			HYDuctSpecifications hyductSpecifications;
			if (this.Option.RectRactorEnable)
			{
				hyductSpecifications = this.Rects.GetMatchSpecificationsByFactor(this.Option.RectFactor, this.Option.RectFactorDeviation);
			}
			else
			{
				hyductSpecifications = this.Rects;
			}
			if (hyductSpecifications == null)
			{
				this.ErrorString = "宽高比设置不合理，过滤不到管径。请重新设置。";
				return false;
			}
			mainSpecs = new HYDuctSpecifications();
			sideSpecs = new HYDuctSpecifications();
			if (this.Option.MainRangeEnable)
			{
				mainSpecs = hyductSpecifications.GetMatchSpecificationsByHeight(this.Option.MainRange);
			}
			else
			{
				mainSpecs = hyductSpecifications;
			}
			if (mainSpecs == null || mainSpecs.Count == 0)
			{
				this.ErrorString = "主管高度范围设置不合理，过滤不到管径，请重新设置。";
				return false;
			}
			if (this.Option.SideRangeEnable)
			{
				sideSpecs = hyductSpecifications.GetMatchSpecificationsByHeight(this.Option.SideRange);
			}
			else
			{
				sideSpecs = hyductSpecifications;
			}
			if (sideSpecs == null || sideSpecs.Count == 0)
			{
				this.ErrorString = "支管高度范围设置不合理，过滤不到管径，请重新设置。";
				return false;
			}
			return true;
		}

		public bool OnDesign()
		{
			this.Option = null;
			this.Option = new HYDuctSystemOption();
			this.Option.Load(Product.UserConfigFile);
			HYDuctSpecifications mainSpecs = null;
			HYDuctSpecifications sideSpecs = null;
			if (!this.FilterSpecifications(out mainSpecs, out sideSpecs))
			{
				return false;
			}
			if (!this.DuctSystem.Design(this.Option, mainSpecs, sideSpecs, this.Rounds, this.m_Rects))
			{
				this.ErrorString = this.DuctSystem.ErrorString;
				return false;
			}
			return true;
		}

		public bool OnCheck()
		{
			this.Option = null;
			this.Option = new HYDuctSystemOption();
			this.Option.Load(Product.UserConfigFile);
			if (!this.DuctSystem.Check(this.Option))
			{
				this.ErrorString = this.DuctSystem.ErrorString;
				return false;
			}
			return true;
		}

		public bool OnOpenFile()
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.InitialDirectory = this.Path;
			openFileDialog.Filter = "鸿业风系统计算文件|*.xml";
			openFileDialog.RestoreDirectory = true;
			openFileDialog.FilterIndex = 0;
			openFileDialog.CheckFileExists = true;
			if (openFileDialog.ShowDialog() != DialogResult.OK)
			{
				return false;
			}
			string fileName = openFileDialog.FileName;
			string fileName2 = System.IO.Path.GetFileName(fileName);
			bool result;
			try
			{
				XElement xelement = XElement.Load(fileName);
				if (xelement == null || xelement.Name != this.XmlRootNode)
				{
					throw new Exception();
				}
				this.FileName = this.FileName;
				result = this.Open();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				this.ErrorString = fileName2 + "不是一个有效的鸿业风管系统计算数据文件";
				result = false;
			}
			return result;
		}

		public bool OnSaveFile()
		{
			if (string.IsNullOrEmpty(this.FileName))
			{
				this.OnSaveAs();
			}
			return this.Save();
		}

		public bool OnSaveAs()
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.InitialDirectory = this.Path;
			saveFileDialog.Filter = "鸿业风系统计算文件|*.xml";
			saveFileDialog.RestoreDirectory = true;
			saveFileDialog.FilterIndex = 0;
			if (saveFileDialog.ShowDialog() != DialogResult.OK)
			{
				return false;
			}
			this.FileName = saveFileDialog.FileName;
			return this.Save();
		}

		public bool OnExcel(BackgroundWorker bkWorker = null)
		{
			HYDuctSystemExcelBook hyductSystemExcelBook = new HYDuctSystemExcelBook(this.DuctSystem, bkWorker);
			if (hyductSystemExcelBook.RunExcel())
			{
				this.ErrorString = "生成Excel计算书成功!";
				return true;
			}
			this.ErrorString = hyductSystemExcelBook.ErrorString;
			return false;
		}

		public bool OnTxt()
		{
			return false;
		}

		public bool OnFormDestroy()
		{
			return false;
		}

		private readonly string XmlRootNode = "HYDuctSystem";

		private int m_Version = 1;

		private HYDuctSpecifications m_Rects;

		private HYDuctSpecifications m_Rounds;

		private HYDuctSpecifications m_AvailableMainRects;

		private HYDuctSpecifications m_AvailableSideRects;
	}
}
